package com.mycompany.app.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.mycompany.app.service.LanguageManager;
import com.mycompany.app.model.Language;
import com.mycompany.app.webapp.action.BaseAction;

import java.util.List;

public class LanguageAction extends BaseAction implements Preparable {
    private LanguageManager languageManager;
    private List languages;
    private Language language;
    private Long  id;
    
    private String sp1;
    private Integer sp2;
    

    public String getSp1() {
    
        return sp1;
    }

    public void setSp1(String sp1) {
    
        this.sp1 = sp1;
    }

    public Integer getSp2() {
    
        return sp2;
    }

    public void setSp2(Integer sp2) {
    
        this.sp2 = sp2;
    }

    public void setLanguageManager(LanguageManager languageManager) {
        this.languageManager = languageManager;
    }

    public List getLanguages() {
        return languages;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String languageId = getRequest().getParameter("language.id");
            if (languageId != null && !languageId.equals("")) {
                language = languageManager.get(new Long(languageId));
            }
        }
    }

    public String list() {
        languages = languageManager.getAll();
        return SUCCESS;
    }

    public void setId(Long  id) {
        this. id =  id;
    }

    public Language getLanguage() {
        return language;
    }

    public void setLanguage(Language language) {
        this.language = language;
    }

    public String delete() {
        languageManager.remove(language.getId());
        saveMessage(getText("language.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            language = languageManager.get(id);
        } else {
            language = new Language();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        
        System.out.println(sp1);
        System.out.println(sp2);
        
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (language.getId() == null);

        languageManager.save(language);

        String key = (isNew) ? "language.added" : "language.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}